package com.rtz.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class StringUtil {

	public static final String NUMBERCHAR = "0123456789";
	public static final String ALLCHAR
    = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	/**
	 * 返回HHmmss + length 长度的纯数字字符串
	 * @param length
	 * @return
	 */
	public static String generateSerialNumber(int length) {
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
		String nowDate = sdf.format(new Date());

		return nowDate + StringUtil.number(length);
	}
	
	public static String generateSerialString(int length) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String nowDate = sdf.format(new Date());

		return nowDate + StringUtil.String(length);
	}
	
	/**
     * 返回一个定长的随机字符串(只包含大小写字母、数字)
     *
     * @param length 随机字符串长度
     * @return 随机字符串
     */
    public static String String(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
        }
        return sb.toString();
    }
	

	/**
	 * 返回固定长度的数字
	 *
	 * @param length
	 * @return
	 */
	public static String number(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(NUMBERCHAR.charAt(random.nextInt(NUMBERCHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 生产UUID
	 * 
	 * @return
	 */
	public static final String generateGUID() {

		String uuid = UUID.randomUUID().toString();
		// 去掉“-”符号
		return uuid.substring(0, 8) + uuid.substring(9, 13)
				+ uuid.substring(14, 18) + uuid.substring(19, 23)
				+ uuid.substring(24);

	}
	/**
	 * 字符串为空(去除首尾空格)
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {

		if (str == null || "".equals(str)) {
			return true;
		} else {
			str = str.trim();
			if (str.length() > 0) {
				return false;
			} else {
				return true;
			}

		}

	}
	/**
	 * 字符串不为空(去除首尾空格)
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {

		if (str == null || "".equals(str)) {
			return false;
		} else {
			str = str.trim();
			if (str.length() > 0) {
				return true;
			} else {
				return false;
			}

		}

	}
	
	public static boolean isNumeric(String str){
		   for (int i = str.length();--i>=0;){  
		       if (!Character.isDigit(str.charAt(i))){
		           return false;
		       }
		   }
		   return true;
		}

}
